//
//  HttpClientTests.swift
//  WalletTests
//
//  Created by Julian on 2022/5/7.
//

import XCTest
import RxSwift
import RxBlocking

class HttpClientTests: XCTestCase {

    let bag = DisposeBag()
    let httpClient = HttpClient()
    
    override func setUpWithError() throws {
        // Put setup code here. This method is called before the invocation of each test method in the class.
    }

    override func tearDownWithError() throws {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
    }
    
    func testHttpClient_getResourcePath() throws {
        // 获取文件内容
        let currenciesURL1 = HttpClient().getResourcePath(with: "currencies", fileExtension: "json")
        XCTAssert(currenciesURL1 != nil, "1. getResourcePath function Error")
        
        let currenciesURL2 = HttpClient().getResourcePath(with: "currency", fileExtension: "json")
        XCTAssert(currenciesURL2 == nil, "2. getResourcePath function Error")
    }
    
    func testHttpClient_request() throws {
        // given
        // when
        let currencyInfo = httpClient.request(CurrencyInfo.self, from: API.currencies)
        let currency = currencyInfo.toBlocking()
        // then
        XCTAssertEqual(try (currency.first()?.currencies.count)! > 0, true)
        XCTAssertEqual(try (currency.first()?.currencies.first?.amount)! > 0, true)
        
        // when
        let liveRates = httpClient.request(TierInfo.self, from: API.liveRetas)
        let rates = liveRates.toBlocking()
        // then
        XCTAssertEqual(try (rates.first()?.tiers.count)! > 0, true)
        XCTAssertEqual(try (rates.first()?.tiers.first?.rates.count)! > 0, true)
        
        // when
        let liveRatesError = httpClient.request(Tiers.self, from: API.liveRetas)
        let ratesError = liveRatesError.toBlocking()
        // then
        XCTAssertThrowsError(try ratesError.first())  // 解析错误
    }
    
    func testExample() throws {
        // This is an example of a functional test case.
        // Use XCTAssert and related functions to verify your tests produce the correct results.
        // Any test you write for XCTest can be annotated as throws and async.
        // Mark your test throws to produce an unexpected failure when your test encounters an uncaught error.
        // Mark your test async to allow awaiting for asynchronous code to complete. Check the results with assertions afterwards.
    }

    func testPerformanceExample() throws {
        // This is an example of a performance test case.
        self.measure {
            // Put the code you want to measure the time of here.
        }
    }

}
